package com.abel.bigwater.data.sanchuan

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.joda.time.DateTime
import org.joda.time.LocalTime
import org.junit.Test
import org.slf4j.LoggerFactory

class MeterOnlineTests {
    /**
     * online-reply: 68 00 12 68 90 00 10 11 02 03 04 05 36 01 20 18 07 18 19 13 58 01 d2 16
     */
    @Test
    fun testOnlineReply() {
        val p = SanchuanPacket().also {
            it.ctrlb = CtrlbType.OnlineReply
            it.actrlb = 0
            it.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)

            it.data = MeterOnlineReply(MeterOnlineReply.FIXED_LEN).apply { serverState = ServerStateType.GameOver }
        }

        val buf = Unpooled.buffer().apply {
            p.fillBuffer(this)

            lgr.info("online-reply: ${BcdString.create(this, readableBytes()).bcdStringBlank()}")
        }
    }

    @Test
    fun testOnlineFillEmpty() {
        val p = SanchuanPacket().also {
            it.ctrlb = CtrlbType.Online
            it.actrlb = 0
            it.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)

            it.data = MeterOnlineData(null).apply {
                sampleTime = DateTime(2018, 6, 1, 2, 3).toDate()
                frameFlag = FrameFlagType(0).apply {
                    this.onlineType = 1
                    this.wmoduleType = WirelessType.BC95HA
                    this.pciTemperature = 1
                }

                volt = 721.0F
                totalOnline = 100
                totalFail = 3

                gmState = GmStateType(0).apply {
                    this.pulseExceptionFlag = 1
                    this.avoidBrokenFlag = 1
                    this.lowVoltLevelOne = 0
                    this.lowVoltLevelTwo = 1
                    this.comFailFlag = 1
                    this.revertFlag = 1
                    this.keyExceptionFlag = 1
                }

                rsrp = -999
                snr = -997
                signalOverlapGrade = 8

                pci = 3
                temperatureDigits = 0x7FFF

                softVersion = SoftVersionType().apply {
                    this.mainVersion = 0x1234
                    this.childVersion = 0x56
                }

                imsi = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)

                totalReading = 12345.67
                totalReservseReading = 78.90

                dayBillData = DayBillDataPart(null).apply {
                    this.count = 0
                    this.flag = DayBillFlagType(0).apply {
                        this.dayBillFormat = 0
                        this.dayBillContent = 0
                    }
                }

                periodReadData = PeriodReadDataPart(null).apply {
                    this.count = 0
                    this.flag = PeriodSumFlag(0).apply { format = PeriodSumFormat.Fmt1 }
                }

                intensiveData = IntensiveDataPart(null).apply {
                    count = 0
                    flag = IntensiveSampleType(0).apply {
                        this.sampleFlag = true
                        this.samplePeriod = SamplePeriodType.Ten
                        this.sampleFormat = 1
                    }
                    startTime = DateTime(2018, 5, 6, 7, 8).toDate()
                }

                warn = WarnDataPart(null).apply {
                    jcount = 0
                    kcount = 0
                }

                maxFlow = 36.9
                maxFlowLocalTime = LocalTime(16, 7, 8)
            }
        }

        val buf = Unpooled.buffer().apply { p.fillBuffer(this) }

        lgr.info("filled:\n${BcdString.create(buf.duplicate(), buf.readableBytes()).bcdStringBlank()}")
    }

    @Test
    fun testOnlineParseEmpty() {
        val msg = """68 00 4e 68 11 00 10 11 02 03 04 05 36 01 20 18 06 01 02 03 00 16 a4 19 64 00 03 00 03 2b 00 00
19 fc 1b fc 08 03 00 ff 7f 34 12 56 10 11 02 03 04 05 36 01 bc cc 5b 07 00 08 0a 0c 00 00 00 00
00 02 00 13 18 05 06 07 08 00 00 24 90 00 00 16 07 08 02 16"""
        val p = SanchuanPacket.parseBuffer(Unpooled.buffer().writeBytes(BcdString.fromBcdString(msg).hexArray))

        lgr.info("parsed sanchuan:\n${JSON.toJSONString(p, true)}")
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(MeterOfflineTests::class.java)
    }
}